package spec.zyh;

import com.sun.org.apache.xpath.internal.operations.Bool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

//@WebServlet(name = "LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
//            String urL = "jdbc:mysql://localhost:3306/1613580003?characterEncoding=utf8";
//            String name = "root";
//            String password = "123456";
//            Class.forName("com.mysql.jdbc.Driver");
//            Connection com = null;
//            com = DriverManager.getConnection(urL, name, password);
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            DatabaseConnetion dbc=new DatabaseConnetion();
            Connection com=dbc.getConnection();
            Statement s = null;
            s = com.createStatement();
            //获取用户名和密码
            String username = request.getParameter("name");
            String pass = request.getParameter("password");
            ResultSet rs=s.executeQuery("select * from t_users where name='"+username+"'");
            //校验用户名和密码是否正确
            if(rs.next()) {
                String b = rs.getString("name");
                String c = rs.getString("password");
                if (b.equals(username) && c.equals(pass)) {
                    //1.使用jdbc从数据库查询username是name
                    HttpSession session = request.getSession();
                    session.setAttribute("name", username);
//                    response.sendRedirect("success.html");
                    ResultSet r=s.executeQuery("select * from t_news");
                    //遍历整个表格
                    response.setContentType("text/html");
                    PrintWriter out1 = response.getWriter();
                    String h="";
                    h +="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" +
                            "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" +
                            "<head>\n" +
                            "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf8\">\n" +
                            "<title>新闻页面</title>\n" +
                            "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n" +
                            "<script src=\"myjs.js\"></script>\n" +
                            "<script type=\"text/javascript\" src=\"common.js\"></script>\n" +
                            "\t<h1 style=text-align:center;>新闻网站</h1>";
                    h +="</head>";
                    h +="<body>";
                    h +="<div id='box'>\n" +
                            "\t<ul id='list'>";
                    h +="\t\t<li><img src=\"11.jpg\"/></li>\n" +
                            "\t\t<li><img src=\"12.jpg\"/></li>\n" +
                            "\t\t<li><img src=\"13.jpg\"/></li>\n" +
                            "\t\t<li><img src=\"14.jpg\"/></li>\n" +
                            "\t\t<li><img src=\"15.jpg\"/></li>\n" +
                            "\t</ul>\n" +
                            "\t<ul id='num'>\n" +
                            "\t\t<li class='active'>1</li>\n" +
                            "\t\t<li>2</li>\n" +
                            "\t\t<li>3</li>\n" +
                            "\t\t<li>4</li>\n" +
                            "\t\t<li>5</li>\t\t\n" +
                            "\t</ul>\n" +
                            "</div>";
                    h +="<form id='form1' name='form1' method='post' action=''>";
                    h +="<label></label>";
                    h +="</form>";
                    h +="<table width='1200' border='0' id='tb'>";
                    h +="<tr align='center'>";
                    h +="<form id='form2' name='form2' method='post' action=''>";
                    h +="<label></label>";
                    h +="</form>";
                    h +="<td width='40px'>选择</td>";
                    h +="<td width='60px'>序号</td>";
                    h +="<td width='500px'>标题</td>";
                    h +="<td width='400px'>发布时间</td>";
                    h +="<td width='100px'>操作1</td>";
                    h +="<td width='100px'>操作2</td>";
                    h +="</tr>";
                    int i=1;
                    while (r.next()) {
                        h +="<tr align='center'>";
                        h +="<td><form  name='form3' method='post' action=''>" + "<label>" +
                            "<input type='checkbox' name='a[]' ;>" + "</label>" + "</form>" + "</td>";
                        h +="<td>" +i++ +"</td>" +"<td><a href=\"http://localhost:8080/ViewServlet?id="+r.getString("id")+"\">" + r.getString("title") + "</a></td>"+"<td>" +r.getString("publish-time")+"</td>";
                        h +="<td>"+"<input type=\"button\" id='btn+"+r.getString("id")+"' onclick='deleteRow(this)' value=\"删除\"  style=\"color:rosybrown; font-size: 12px;\">"+"</td>";
                        h +="<td><a href=\"http://localhost:8080/RviewServlet?id="+r.getString("id")+"\">修改</a></td>";
                        h +="</tr>";
                    }
                    h +="</table>";
                    h +="<div id='bu'>";
                    h +="<input type=\"button\" value=\"增加\" id=\"z\" style=\"color: blueviolet; font-size: 16px;margin-left:10px;margin-right: 10px;\">";
//                    h +="<input type=\"button\" value=\"查看\" id=\"c\" style=\"color: darkorange; font-size: 16px;margin-left: 20px;margin-right: 20px;\"><br><br>";
                    h +="</div>";
                    h +=" <script type=\"text/javascript\">\n" +
                            "        var b = document.getElementById(\"z\");\n" +
                            "        var d = document.getElementById(\"c\");\n" +
                            "        b.onclick=function () {\n" +
                            "            window.open('add.html');\n" +
                            "        }\n" +
                            "            function deleteRow(o) {\n" +
                            "                   var btnId = o.id;  \n" +
                            "                   var id = btnId.substring(4);\n" +
                            "                    var u=\"http://localhost:8080/DeleteServlet?id=\"+id;\n" +
//                            "                    var tr=o.parentNode.parentNode;\n"+
//                           "                    tr.parentNode.removeChild(tr);\n" +
                            "                    window.open(u);\n" +
                            "             }\n" +
                            "    </script>";
                    h +="</body>";
                    h +="</html>";
                    out1.println(h);
                } else {
                    response.sendRedirect("login.html");//重定向
                     //设置响应内容类型
                }
            }
            dbc.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应内容类型
        //response.setContentType("text/html");
        //设置逻辑实现
//        PrintWriter out=response.getWriter();
//        out.print("<h1>"+"Hello Servlet"+"<h1>");
    }
}
